<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      title="商家主动完成订单"
      width="750px"
      append-to-body
      @close="close"
    >
      <el-form
        v-if="detail"
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="订单编号：">{{ detail.orderNo }}</el-form-item>
        <el-form-item label="下单时间：">{{ detail.createTime }}</el-form-item>
        <el-form-item label="下单顾客：">{{ detail.memberName||detail.customerName||detail.customerId }}</el-form-item>
        <el-divider />
        <el-form-item label="备注：" prop="remark">
          <el-input
            v-model="form.remark"
            :autosize="{ minRows: 2, maxRows: 6}"
            type="textarea"
            maxlength="100"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div class="flexCCC">
          <el-button
            :disabled="loading"
            size="large"
            round
            type="primary"
            class="btn-submit"
            @click="submit"
          >确认完成</el-button>
          <el-button :disabled="loading" size="text" round @click="hide">取 消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { confirmOrder } from '@/views/wxaMallModule/api/order'
export default {
  data() {
    return {
      // 校验字段
      rules: {
        // trackingNo: [
        //   {
        //     required: true,
        //     message: "请输入物流单号",
        //     trigger: ["blur", "change"],
        //   },
        // ],
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      detail: null,
      form: {
        orderNo: null,
        remark: null
      }
    }
  },
  methods: {
    /**
     * 弹框相关
     */
    open({ detail }) {
      this.detail = detail
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.form = {
        orderNo: null,
        remark: null
      }
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */

    /**
     * 按钮组
     */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          this.form.orderNo = this.detail.orderNo
          confirmOrder(this.form)
            .then((res) => {
              this.$notify({
                title: '成功',
                message: '订单已完成',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch((err) => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
    /**
     * 状态组
     */

    /**
     * 方法组
     */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.w-400 {
  width: 400px;
  display: block;
  margin: 0 auto;
}
.btn-submit {
  margin-bottom: 20px;
}
</style>
